//
//  WelcomeViewController.m
//  Demo3_欢迎界面
//
//  Created by tarena on 15/11/12.
//  Copyright (c) 2015年 tarena. All rights reserved.
//

#import "ZYYWelcomeViewController.h"
#import "Masonry/Masonry.h"
#import "ZYYRootViewController.h"
@interface ZYYWelcomeViewController ()<UIScrollViewDelegate>


@end

@implementation ZYYWelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupScrollView];
    
}

// 定制滚动视图
-(void)setupScrollView
{
    // 创建滚动视图
    UIScrollView *sv = [[UIScrollView alloc]init];
    
    // 为了捕获滚动视图与用户的交互，需要设置代理
    sv.delegate = self;
    
    // 设置边缘不能弹跳
    sv.bounces = NO;
    
    // 设置滚动视图整页滚动
    sv.pagingEnabled = YES;
    
    // 设置水平滚动条不可见
    sv.showsHorizontalScrollIndicator = NO;
    
    // 设置滚动视图的可见区域
    sv.frame = self.view.bounds;
    
    // 设置contentSize
    sv.contentSize = CGSizeMake(sv.bounds.size.width*4, sv.bounds.size.height);

    // 添加子视图
    for (NSInteger i=0; i<4; i++)
    {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.frame = CGRectMake(sv.bounds.size.width*i, 0, sv.bounds.size.width, sv.bounds.size.height);
        
        NSString *fileName = [NSString stringWithFormat:@"yindao%ld.png",i+1];
        imageView.image = [UIImage imageNamed:fileName];
        [sv addSubview:imageView];
        
        //如果是最后一幅图片，则向其中添加一个按钮
        if (i==3) {
            imageView.userInteractionEnabled = YES;
            /*UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoMain:)];
            tap.numberOfTapsRequired = 1;
            tap.numberOfTouchesRequired = 1;
            [imageView addGestureRecognizer:tap];*/
            
            UIButton *button = [UIButton buttonWithType:0];
            [button setBackgroundImage:[UIImage imageNamed:@"stepin_normal"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"stepin_selected"] forState:UIControlStateHighlighted];
            [button addTarget:self action:@selector(gotoMain) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:button];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(-60);
                make.centerX.mas_equalTo(0);
            }];
            
        }
    }
    // 添加滚动视图到控制器的view中
    [self.view addSubview:sv];
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取滚动位置的偏移量
    //CGPoint point = scrollView.contentOffset;
    //计算偏移量是滚动视图宽度的整数倍
    //为了在超过一半时，就自动是下一个圆点
    //通过round函数四舍五入即可
    //self.pageControl.currentPage = round(point.x/scrollView.bounds.size.width);
}

// 配置点击进入程序的按钮
-(void)gotoMain
{
    
    ZYYRootViewController *rootVC = [[ZYYRootViewController alloc]init];
    
    //更换window的根视图为mainVC
    //欢迎界面不再是根视图以后，就会被系统回收
    
    //获取在main函数中创建过的那个唯一的应用程序对象
    UIApplication *application = [UIApplication sharedApplication];
    UIWindow *window = application.keyWindow;
    
    //更换根vc
    window.rootViewController = rootVC;
    
    [[NSUserDefaults standardUserDefaults] setValue:@1 forKey:@"isFirstUseApp"];

}




@end
